﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace ParallelCacheHandler
{
    public sealed class CacheKeyGenerator
    {
        public static string GetCacheKey(IMethodInvocation input)
        {
            string key = string.Empty;
            foreach (var arg in input.Arguments)
            {
                //checking whether the parameter can be safely used for a hashcode based comparison (i.e value types and types that have explicitly 
                //implemented the IEquatable interface for comparison
                var argType = arg.GetType();
                if (typeof(IEquatable<>).MakeGenericType(argType).IsAssignableFrom(argType))
                {
                    key += "|" + arg.GetHashCode() + "|";
                }
                else
                {
                    throw new CacheKeyException(input, arg);
                }
            }

            return input.MethodBase + key;
        }

    }
}
